package com.lz.football.module.compete
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ClubConst;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.constants.TipsConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.compete.item.NationRankItem;
	import com.lz.football.module.compete.matchResult.MatchResultListView;
	
	import common.BaseView.BaseView;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.managers.TipsManager;
	
	public class CompeteView extends BaseView implements ICompete
	{
		/**
		 *背景 
		 */
		private var _bg:MovieClip;
		/**
		 * 挑战面板
		 */
		private var _challengeView:ChallengeView;
		
		/**
		 *背景容器 
		 */
		private var _bgSprite:Sprite;
		/**
		 *地图 
		 */
		private var _mapView:MapView;
		
		
		/**
		 *奖励按钮 
		 */
		private var awardBtn:SimpleButton;
		/**
		 *任务 
		 */
		private var taskBtn:SimpleButton;
		/**
		 *宝库 
		 */
		private var treasuryBtn:SimpleButton;
		/**
		 *离开 
		 */
		private var leaveBtn:SimpleButton;
		/**
		 *宝库界面 
		 */
		private var _treasuryView:TreasuryView;
		/**
		 *任务 
		 */
		private var _taskView:CompeteTaskView;
		/**
		 *国内排名 
		 */
		private var _inlandRankView:InlandRankView;
		/**
		 *理多的排名  
		 */
		private var moreRankBtn:SimpleButton;
		/**
		 *规则 
		 */
		private var ruleBtn:SimpleButton;
		/**
		 *个人排名  
		 */
		private var selfRank:TextField;
		/**
		 *国家排名item 
		 */		
		private var _nationRankItemVec:Vector.<NationRankItem>;
		/**
		 * 
		 */
		private var _changellView:SingleChaglleView;
		
		/**
		 *国内比赛列表 
		 */
		private var _matchResultList:MatchResultListView;
		/**
		 *规则面板 
		 */
		private var _rule:CompeteRuleView;
		
		/**
		 *奖励面板 
		 */
		private var _awardView:AwardView;
		
		public function CompeteView()
		{
			super();
		}
		/**
		 *背景 
		 * @return 
		 * 
		 */		
		public function get bg():DisplayObject
		{
			if(_bgSprite == null)
			{
				_bgSprite = new Sprite();
				
			}
			return _bgSprite;
		}
		
		protected function onClickBgHandler(event:MouseEvent):void
		{
			var str:String = event.target.name;
			if("country" !=  str.split("_")[0])return;
			if(_challengeView == null)
			{
				_challengeView = new ChallengeView();
				_challengeView.initUI();
			}
			var id:int = str.split("_")[1];
			_challengeView.selectCountry(id);
			
			this.dispatchEvent(new CompeteEvent(CompeteEvent.KANDSENIOR,{id:id}));
		}
		/**
		 * 
		 * @param view
		 * 
		 */
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.compete.infoView");
			symbolSkin.cacheAsBitmap = true;
			taskBtn = symbolSkin.btn.taskBtn;
			treasuryBtn = symbolSkin.btn.treasuryBtn;
			leaveBtn = symbolSkin.btn.leaveBtn;
			awardBtn = symbolSkin.btn.awardBtn
			taskBtn.visible = false;
			treasuryBtn.visible = false;
			symbolSkin.selfRank.visible = false;
			symbolSkin.nationRank.visible = false;
			symbolSkin.tile.visible = false;
			moreRankBtn = symbolSkin.selfRank.moreRankBtn;
			ruleBtn = symbolSkin.selfRank.ruleBtn;
			selfRank = symbolSkin.selfRank.selfRank ;
			
			
			_matchResultList = new MatchResultListView();
			_matchResultList.initUI();
			this.addChild(_matchResultList);
			this.addChild(symbolSkin);
			//setData();
//			checkMatchTime();
			addEvtListener();
			onResizeHandler();
			initNationItem();
			
			
			//_matchResultList.addItem(null);
			
			
		}
		/**
		 *初始化国家排名 
		 * 
		 */
		private function initNationItem():void
		{
			var item:NationRankItem;
			_nationRankItemVec = new Vector.<NationRankItem>();
			for(var i:int = 0;i < 6;i++)
			{
				item = new NationRankItem();
				item.initUI();
				item.x = 23;
				item.y = item.height * i + 28;
				symbolSkin.nationRank.addChild(item);
				_nationRankItemVec.push(item);
			}
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			leaveBtn.addEventListener(MouseEvent.CLICK,onCloseHandler);
			taskBtn.addEventListener(MouseEvent.CLICK,onTaskHandler);
			treasuryBtn.addEventListener(MouseEvent.CLICK,ontreasuryHanlder);
			moreRankBtn.addEventListener(MouseEvent.CLICK,onMoreRankHandler);
			selfRank.addEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			selfRank.addEventListener(MouseEvent.ROLL_OVER,onoverHandler);
			ruleBtn.addEventListener(MouseEvent.CLICK,onRuleHandler);
			awardBtn.addEventListener(MouseEvent.CLICK,onAwardhandler);
		}
		
		/**
		 *奖励界面 
		 * @param event
		 * 
		 */
		protected function onAwardhandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_awardView == null)
			{
				_awardView = new AwardView();
				_awardView.initUI();
				
			}
			_awardView.setDate();
			GlobalDef.AlertContainer.addChild(_awardView);
		}
		
		/**
		 *规则 
		 * @param event
		 * 
		 */
		protected function onRuleHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(_rule == null)
			{
				_rule = new CompeteRuleView();
				_rule.initUI();
			}
			GlobalDef.AlertContainer.addChild(_rule);
		}
		
		protected function onoverHandler(event:MouseEvent):void
		{
			TipsManager.show(TipsConst.BASETIPS,{tip:AppData.getInstance().competeDataManager.competeInfoVo.rival},15,15,true);
		}
		
		protected function onOutHandler(event:MouseEvent):void
		{
			TipsManager.hide();
		}
		
		protected function onMoreRankHandler(event:MouseEvent):void
		{
			if(_inlandRankView == null)
			{
				_inlandRankView = new InlandRankView();
				_inlandRankView.initUI();
			}
			GlobalDef.AlertContainer.addChild(_inlandRankView);
			if(AppData.getInstance().competeDataManager.previousList == null)
			{
				this.dispatchEvent(new CompeteEvent(CompeteEvent.MOREINLANDRANK,{type:2}));
			}else
			{
				if(AppData.getInstance().time - AppData.getInstance().competeDataManager.moreinlandRankTime > 1800)
					this.dispatchEvent(new CompeteEvent(CompeteEvent.MOREINLANDRANK,{type:1}));
				else
					moreinlandRank(null);
			}
			
			
			
			
		}
		
		/**
		 *宝库界面 
		 * @param event
		 * 
		 */
		protected function ontreasuryHanlder(event:MouseEvent):void
		{
			if(_treasuryView == null)
			{
				_treasuryView =  new TreasuryView();
				_treasuryView.initUI();
			}
			GlobalDef.AlertContainer.addChild(_treasuryView);
		}
		
		/**
		 *任务界面  
		 * @param event
		 * 
		 */
		protected function onTaskHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.GETTREASURYFUNDS));
			
			
			
			if(_taskView == null)
			{
				_taskView = new CompeteTaskView();
				_taskView.initUI();
				_taskView.upDateNation();
				_taskView.type = 1;
			}
			GlobalDef.AlertContainer.addChild(_taskView);
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			super.onCloseHandler(event);
			this.dispatchEvent(new CompeteEvent(ModuleEvent.REMOVE_MODULE));
		}
		/**
		 *自适应 
		 * @param event
		 * 
		 */		
		override public function onResizeHandler(event:Event=null):void
		{
			symbolSkin.selfRank.x = 5;
			symbolSkin.selfRank.y = 80;
			symbolSkin.nationRank.x = GlobalDef.StageWidth - 150;
			symbolSkin.nationRank.y = 50;
			symbolSkin.btn.x = GlobalDef.StageWidth - symbolSkin.btn.width - 10;
			symbolSkin.btn.y = GlobalDef.StageHeight - symbolSkin.btn.height - 20;
			symbolSkin.tile.x = (GlobalDef.StageWidth - 330)>>1;
			symbolSkin.tile.y = 20;
			_matchResultList.x = 5;
			_matchResultList.y = 350;
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			leaveBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			taskBtn.removeEventListener(MouseEvent.CLICK,onTaskHandler);
			treasuryBtn.removeEventListener(MouseEvent.CLICK,ontreasuryHanlder);
			moreRankBtn.removeEventListener(MouseEvent.CLICK,onMoreRankHandler);
			selfRank.removeEventListener(MouseEvent.ROLL_OUT,onOutHandler);
			selfRank.removeEventListener(MouseEvent.ROLL_OVER,onoverHandler);
			ruleBtn.removeEventListener(MouseEvent.CLICK,onRuleHandler);
			awardBtn.removeEventListener(MouseEvent.CLICK,onAwardhandler);
		}
		override public function destroyView():void
		{
			removeEvtListener();
			if(_challengeView != null)_challengeView.destroyView();
			_challengeView = null;
			if(_mapView != null)_mapView.destroyView();
			_mapView= null;
			if(_treasuryView != null)_treasuryView.destroyView();
			_treasuryView = null;
			if(_taskView != null)_taskView.destroyView();
			_taskView = null;
			if(_inlandRankView != null)_inlandRankView.destroyView();
			_inlandRankView= null;
			
			
			if(_matchResultList != null)_matchResultList.destroyView();
			_matchResultList= null;
			if(_changellView != null)_changellView.destroyView();
			_changellView= null;
			
			if(_awardView != null)_awardView.destroyView();
			_awardView = null;
			if(_rule != null)
				_rule.destroyView();
			_rule= null;
			super.destroyView();
		}
		 
		/**
		 *检查比赛时间 
		 * 
		 */
		private function checkMatchTime():void
		{
			DisplayObjectUtil.removeAllChildren(_bgSprite);
			var date:Date = AppData.getInstance().curDate;
			if(date.getDay() == 1 && date.getHours()<=18)//每个星期1 16点前
			{
				if(AppData.getInstance().competeDataManager.competeInfoVo.rivalcountry != 0)
				{
					setMapView();
				}else
				{
					setSelectView();
				}
				
			} else
			{
				setMapView();
			}
//			setSelectView();
			//lake 131026
			if(_bgSprite.parent)
			{
				_bgSprite.parent.x = ( GlobalDef.GameStage.stageWidth - _bgSprite.width)/2;
				_bgSprite.parent.y = ( GlobalDef.GameStage.stageHeight - _bgSprite.height)/2;
				if((Math.abs(_bgSprite.parent.x) + GlobalDef.GameStage.stageWidth)>_bgSprite.parent.width)
				{
					_bgSprite.parent.x = GlobalDef.GameStage.stageWidth - _bgSprite.parent.width;
				}
				if((Math.abs(_bgSprite.parent.y) + GlobalDef.GameStage.stageHeight)>_bgSprite.parent.height)
				{
					_bgSprite.parent.y = GlobalDef.GameStage.stageHeight - _bgSprite.parent.height;
				}
			}
		}
		
		/**
		 * 设置选择国家挑战面板
		 * 
		 */
		private function setSelectView():void
		{
			bg;
			_bg = Reflection.createInstance("com.lz.modul.compete.nationSelectView");
			_bgSprite.addChild(_bg);
			_bgSprite.addEventListener(MouseEvent.CLICK,onClickBgHandler);
			setHide(false);
		}
		/**
		 * 设置地图面板
		 * 
		 */
		private function setMapView():void
		{
			bg;
			if(_mapView == null)
			{
				_mapView = new MapView();
				_mapView.initUI();
			}
			
			_bgSprite.addChild(_mapView);
			setHide(true);
		}
		/**
		 *设置显示状态  
		 * 
		 */
		private function setHide($bool:Boolean):void
		{
			symbolSkin.selfRank.visible = $bool;
			symbolSkin.nationRank.visible = $bool;
			taskBtn.visible = $bool;
			treasuryBtn.visible = $bool;
			symbolSkin.tile.visible = $bool;
		}
		/**
		 *国王和三大将信息 
		 * 
		 */
		public function tkingandsenior($obj:Object):void
		{
			if(_challengeView != null)
			{
				_challengeView.setData($obj);
				GlobalDef.AlertContainer.addChild(_challengeView);
				
			}
		}
		/**
		 *更新对手列表 
		 * 
		 */
		public function updataOppList():void
		{
			checkMatchTime();
			if(_mapView != null && _mapView.parent)
			{
				_mapView.updataList();
				var rank:int =  AppData.getInstance().competeDataManager.competeInfoVo.nationrank;
				if(rank < 10)
				{
//					selfRank.htmlText = "<font size = '100'>" +rank +  "</font>";
				}else if(rank < 100)
				{
//					selfRank.htmlText = "<font size = '80'>" +rank +  "</font>";
				}else if(rank < 1000)
				{
//					selfRank.htmlText = "<font size = '60'>" +rank +  "</font>";
				}else
				{
//					selfRank.htmlText = "<font size = '50'>" +rank +  "</font>";
				}
//				selfRank.text = AppData.getInstance().competeDataManager.competeInfoVo.nationrank + "";
				nationRank();
				competeTile();
//				selfRank.width = selfRank.textWidth + 5;
//				selfRank.height = selfRank.textHeight ;
				selfRank.htmlText = rank + ""//"<font size = '50'>" +rank +  "</font>";
			}
			if(_challengeView != null && _challengeView.parent)
				_challengeView.parent.removeChild(_challengeView);
		}
		
		/**
		 *国家排名 
		 * 
		 */
		private function nationRank():void
		{
			var arr:Array = AppData.getInstance().competeDataManager.nationRankList;
			var len:int = arr.length > _nationRankItemVec.length?_nationRankItemVec.length:arr.length;
			for(var i:int = 0;i < len;i++)
			{
				_nationRankItemVec[i].setData(arr[i]);
			}
		}
		/**
		 *挑战赛标题 
		 * 
		 */
		private function competeTile():void
		{
			symbolSkin.tile.right.text = ClubConst.countryToString(AppData.getInstance().competeDataManager.competeInfoVo.rivalcountry);
			symbolSkin.tile.rightIcon.gotoAndStop(AppData.getInstance().competeDataManager.competeInfoVo.rivalcountry);
			
			
			symbolSkin.tile.leftIcon.gotoAndStop(AppData.getInstance().clubDatamanager.clubInfo.country);
			symbolSkin.tile.left.text = ClubConst.countryToString(AppData.getInstance().clubDatamanager.clubInfo.country);
			
			
			
			var xml:XML = ResourceManager.instance.getResource(ResourceConst.LADDERMATCHTIME);
			
			var str:String = xml.competeRoundTime
			var f:String = xml.timeInterval;
			symbolSkin.tile.roundTime.text = "本轮国家挑战赛时间：" + DateUtil.getnatureWeek(AppData.getInstance().curDate);//str + f
//			symbolSkin.tile.timeInterval.text = xml.timeInterval;
		}
		/**
		 *更新自己的排名  
		 * 
		 */
		public function updataselfRank():void
		{
			
		}
		/**
		 *国内排名列表 
		 * 
		 */
		public function inlandRankList():void
		{
			
		}
		/**
		 *其他俱乐部信息 
		 * @param $obj
		 * 
		 */
		public function otherInfo($obj:Object):void
		{
			if( _changellView == null)
			{
				_changellView = new SingleChaglleView();
				_changellView.initUI();
			}
			_changellView.setData($obj.vo);
			GlobalDef.AlertContainer.addChild(_changellView);
		}
		/**
		 *捐赠返回更新国库资金 
		 * 
		 */
		public function getdonate():void
		{
			if(_taskView != null)
			{
				_taskView.updata();
				_taskView.upDateNation();
			}
		}		
		/**
		* 国内比赛结果 
		* @param $obj
		* 
		*/
		public function getMatchResult($obj:Object):void
		{
			_matchResultList.addItem($obj);
		}
		/**
		 *获得更多的排名 
		 * @param $obj
		 * 
		 */
		public function moreinlandRank($obj:Object):void
		{
			if(_inlandRankView != null && _inlandRankView.parent)_inlandRankView.upData();
			if(_mapView != null )
			{
				_mapView.updataList();
				var rank:int =  AppData.getInstance().competeDataManager.competeInfoVo.nationrank;
//				if(rank < 10)
//				{
//					selfRank.htmlText = "<font size = '100'>" +rank +  "</font>";
//				}else if(rank < 100)
//				{
//					selfRank.htmlText = "<font size = '80'>" +rank +  "</font>";
//				}else if(rank < 1000)
//				{
//					selfRank.htmlText = "<font size = '60'>" +rank +  "</font>";
//				}else
//				{
//					selfRank.htmlText = "<font size = '50'>" +rank +  "</font>";
//				}
			}
			selfRank.htmlText =rank + ""// "<font size = '50'>" +rank +  "</font>";
		}
		/**
		 *返回挑战 
		 * 
		 */
		public function getChallange():void
		{
			if(_changellView != null)_changellView.addEvtListener();
		}
	}
}